package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.impl;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Doctor;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Linshi2;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.UserTable;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.mapper.DoctorMapper;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.DoctorService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 分页查询Doctor信息
     *
     * @param page      当前页码
     * @param pageSize  每页条数
     * @param doctorName 查询条件，医生名
     * @return 分页结果
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String doctorName) {
        PageHelper.startPage(page, pageSize);
        List<Linshi2> doctorList = doctorMapper.list(doctorName);
        Page<Linshi2> p = (Page<Linshi2>) doctorList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 根据doctorID删除Doctor信息
     *
     * @param doctorID 医生ID
     */
    @Override
    public void delete(Integer doctorID) {
        Integer user = doctorMapper.getUserID2(doctorID);
        doctorMapper.delete(doctorID);
        doctorMapper.deleteUser(user);
    }

    /**
     * 新增Doctor信息
     *
     * @param linshi2 包含Doctor信息的临时对象
     */
    @Override
    public void add(Linshi2 linshi2) {
        Doctor doctor = new Doctor();
        UserTable user = new UserTable();
        user.setUsername(linshi2.getUsername());
        user.setPassword(linshi2.getPassword());
        user.setRole(1);
        doctorMapper.insertUser(user);
        doctor.setUserID(doctorMapper.getUserID(linshi2.getUsername()));
        doctor.setDoctorName(linshi2.getDoctorName());
        doctor.setDepartmentID(doctorMapper.getDepartmentID(linshi2.getDepartmentName()));
        doctorMapper.insert(doctor);
    }

    /**
     * 更新Doctor信息
     *
     * @param linshi2 包含Doctor信息的临时对象
     */
    @Override
    public void update(Linshi2 linshi2) {
        Doctor doctor = new Doctor();
        UserTable user = new UserTable();
        user.setUsername(linshi2.getUsername());
        user.setPassword(linshi2.getPassword());
        user.setUserID(doctorMapper.getUserID2(linshi2.getDoctorID()));
        doctorMapper.updateUser(user);
        doctor.setDoctorID(linshi2.getDoctorID());
        doctor.setDoctorName(linshi2.getDoctorName());
        doctor.setDepartmentID(doctorMapper.getDepartmentID(linshi2.getDepartmentName()));
        log.info("医生信息:{}", doctor);
        doctorMapper.updateDoctor(doctor);
    }
}
